package bosc.cn.controller;

import bosc.cn.api.WalletApi;
import bosc.cn.converter.wallet.*;
import bosc.cn.dto.common.CommonResponse;
import bosc.cn.dto.common.RequestWrapper;
import bosc.cn.dto.common.ResponseWrapper;
import bosc.cn.dto.wallet.*;
import bosc.cn.service.wallet.WalletDetailService;
import bosc.cn.service.wallet.WalletLimitMaintainService;
import bosc.cn.service.wallet.WalletListService;
import bosc.cn.service.wallet.WalletRegisterService;
import bosc.cn.service.wallet.bo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/wallet")
@Slf4j
public class WalletController implements WalletApi {

    @Autowired
    private WalletRegisterService walletRegisterService;

    @Autowired
    private WalletDetailService walletDetailService;

    @Autowired
    private WalletListService walletListService;

    @Autowired
    private WalletLimitMaintainService walletLimitMaintainService;

    @Override
    @PostMapping("/doAuth")
    public ResponseWrapper<WalletRegisterResDTO> walletRegister(
            @RequestBody RequestWrapper<WalletRegisterReqDTO> reqWrapper) {

        log.info("接受请求：{}", reqWrapper);
        WalletRegisterReqDTO body = reqWrapper.getBody();
        // DTO -> BO
        WalletRegisterReqBO reqBO = WalletRegisterReqConverter.toBO(body);

        ResponseWrapper<WalletRegisterResBO> resWrapper = walletRegisterService.walletRegister(reqBO);
        // 从 Service 返回结果中取出 head 和 body
        CommonResponse head = resWrapper.getHead();
        WalletRegisterResBO resBO = resWrapper.getBody();
        // BO -> DTO
        WalletRegisterResDTO resDTO = WalletRegisterResConverter.toDTO(resBO);

        ResponseWrapper<WalletRegisterResDTO> responseWrapper = new ResponseWrapper<>();
        responseWrapper.setHead(head);
        responseWrapper.setBody(resDTO);
        log.info("返回响应：{}", responseWrapper);

        return responseWrapper;
    }

    @PostMapping("/doQueryDetail")
    public ResponseWrapper<WalletDetailResDTO> walletDetail(
            @RequestBody RequestWrapper<WalletDetailReqDTO> reqWrapper) {

        log.info("接受请求：{}", reqWrapper);
        WalletDetailReqDTO body = reqWrapper.getBody();
        // DTO -> BO
        WalletDetailReqBO reqBO = WalletDetailReqConverter.toBO(body);

        ResponseWrapper<WalletDetailResBO> resWrapper = walletDetailService.getWalletDetail(reqBO);
        // 从 Service 返回结果中取出 head 和 body
        CommonResponse head = resWrapper.getHead();
        WalletDetailResBO resBO = resWrapper.getBody();
        // BO -> DTO
        WalletDetailResDTO resDTO = WalletDetailResConverter.toDTO(resBO);

        ResponseWrapper<WalletDetailResDTO> responseWrapper = new ResponseWrapper<>();
        responseWrapper.setHead(head);
        responseWrapper.setBody(resDTO);

        log.info("返回响应：{}", responseWrapper);
        return responseWrapper;
    }

    @PostMapping("/doQueryList")
    public ResponseWrapper<WalletListResDTO> walletList(
            @RequestBody RequestWrapper<WalletListReqDTO> reqWrapper) {

        log.info("接受请求：{}", reqWrapper);
        WalletListReqDTO body = reqWrapper.getBody();
        // DTO -> BO
        WalletListReqBO reqBO = WalletListReqConverter.toBO(body);

        ResponseWrapper<WalletListResBO> resWrapper = walletListService.getWalletList(reqBO);
        // 从 Service 返回结果中取出 head 和 body
        CommonResponse head = resWrapper.getHead();
        WalletListResBO resBO = resWrapper.getBody();
        // BO -> DTO
        WalletListResDTO resDTO = WalletListResConverter.toDTO(resBO);

        ResponseWrapper<WalletListResDTO> responseWrapper = new ResponseWrapper<>();
        responseWrapper.setHead(head);
        responseWrapper.setBody(resDTO);

        log.info("返回响应：{}", responseWrapper);
        return responseWrapper;

    }

    @PostMapping("/doUpdateInfo")
    public ResponseWrapper<WalletLimitMaintainResDTO> walletLimit(
            @RequestBody RequestWrapper<WalletLimitMaintainReqDTO> reqWrapper) {

        log.info("接受请求：{}", reqWrapper);
        WalletLimitMaintainReqDTO body = reqWrapper.getBody();
        // DTO -> BO
        WalletLimitMaintainReqBO reqBO = WalletLimitMaintainReqConverter.toBO(body);
        // 调用 Service，返回 ResponseWrapper<WalletLimitMaintainResBO>
        ResponseWrapper<WalletLimitMaintainResBO> resWrapper = walletLimitMaintainService.updateWalletLimit(reqBO);
        // 从 Service 返回结果中取出 head 和 body
        CommonResponse head = resWrapper.getHead();
        WalletLimitMaintainResBO resBO = resWrapper.getBody();
        // BO -> DTO
        WalletLimitMaintainResDTO resDTO = WalletLimitMaintainResConverter.toDTO(resBO);

        ResponseWrapper<WalletLimitMaintainResDTO> responseWrapper = new ResponseWrapper<>();
        responseWrapper.setHead(head);
        responseWrapper.setBody(resDTO);

        log.info("返回响应：{}", responseWrapper);
        return responseWrapper;

    }

}

